package com.example.tellwin.mine.act;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.event.ExpenseCalendarEvent;
import com.example.tellwin.event.PayResultEvent;
import com.example.tellwin.mine.adapter.ExpenseRecordAdapter;
import com.example.tellwin.mine.bean.ExpenseRecordBean;
import com.example.tellwin.mine.bean.UserContentBean;
import com.example.tellwin.mine.bean.UserInfo;
import com.example.tellwin.mine.contract.WalletContract;
import com.example.tellwin.mine.presenter.WalletPresenter;
import com.example.tellwin.utils.CommonUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WalletActivity extends CpBaseActivty implements WalletContract.View {

    @BindView(R.id.balance_tv)
    TextView balanceTv;
    @BindView(R.id.recharge_tv)
    TextView rechargeTv;
    @BindView(R.id.share_record_tv)
    TextView shareRecordTv;
    @BindView(R.id.expense_record_rv)
    RecyclerView expenseRecordRv;
    @BindView(R.id.mine_ali_tv)
    TextView mineAliTv;
    @BindView(R.id.mine_band_card_tv)
    TextView mineBandCardTv;
    @BindView(R.id.answer_wallet_rl)
    RelativeLayout answerWalletRl;
    @BindView(R.id.record_text)
    TextView recordText;

    ExpenseRecordAdapter expenseRecordAdapter;
    List<ExpenseRecordBean> expenseRecordBeans = new ArrayList<>();

    @Inject
    WalletPresenter mPresenter;

    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {

        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        expenseRecordRv.setLayoutManager(linearLayoutManager);
        expenseRecordRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_7);
            }
        });
        expenseRecordAdapter = new ExpenseRecordAdapter();
        expenseRecordRv.setAdapter(expenseRecordAdapter);
    }

    @Override
    protected void initEvents() {
        expenseRecordRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (CommonUtils.isSlideToBottom(expenseRecordRv)) {
                    mPresenter.loadMore();
                }
            }
        });
    }

    @Override
    protected void initDatas() {
        setTitle(R.string.shu_xiu);
        userInfo = (UserInfo) getIntent().getSerializableExtra(Common.USERINFO);
        mPresenter.getWalletDetail();
        if (MainApplication.Identity.equals(Common.NORMAL)) {
            answerWalletRl.setVisibility(View.GONE);
            shareRecordTv.setVisibility(View.VISIBLE);
            recordText.setText(R.string.expense_record);
            rechargeTv.setText(R.string.recharge);
        } else if (MainApplication.Identity.equals(Common.ANSWER)) {
            answerWalletRl.setVisibility(View.VISIBLE);
            shareRecordTv.setVisibility(View.GONE);
            recordText.setText(R.string.income_record);
            rechargeTv.setText("提现");
        }
        balanceTv.setText(userInfo.getBalance() + "");
    }

    @OnClick({R.id.recharge_tv, R.id.share_record_tv, R.id.mine_ali_tv, R.id.mine_band_card_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.recharge_tv:
                if (MainApplication.Identity.equals(Common.NORMAL)) {
                    Intent intent = new Intent(this, RechargeActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(this, WithdrawMoneyActivity.class);
                    // intent.putExtra(Common.USERINFO ,userInfo);
                    startActivity(intent);
                }
                break;
            case R.id.share_record_tv:
                Intent intent = new Intent(WalletActivity.this , ShareRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.mine_ali_tv:
                Intent intent2 = new Intent(this, BindActivity.class);
                intent2.putExtra(Common.TYPE, 1);
                intent2.putExtra(Common.USERINFO, userInfo);
                startActivity(intent2);
                break;
            case R.id.mine_band_card_tv:
                Intent intent1 = new Intent(this, BindActivity.class);
                intent1.putExtra(Common.TYPE, 0);
                intent1.putExtra(Common.USERINFO, userInfo);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void walletDetailResult(List<ExpenseRecordBean> expenseRecordBeans) {
        this.expenseRecordBeans = expenseRecordBeans;
        expenseRecordAdapter.setExpenseRecordBeans(expenseRecordBeans);
    }

    @Override
    public void loadMoreResult(List<ExpenseRecordBean> expenseRecordBean) {
        this.expenseRecordBeans.addAll(expenseRecordBean);
        expenseRecordAdapter.notifyDataSetChanged();
    }

    @Override
    public void userInfoResult(UserContentBean userContentBean) {
        userInfo = userContentBean.getUserInfo();
        balanceTv.setText(userInfo.getBalance() + "");
    }

    @Subscribe
    public void onEvent(ExpenseCalendarEvent event) {
        mPresenter.getWalletDetail();
        mPresenter.getUserInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
